﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using AutoMapper;

namespace Data
{
  public  class BaoCaoCongNoRepository
    {
      private static BaoCaoCongNoDTO MapBaoCaoCongNoEFToBaoCaoCongNoDTO(BAOCAOCONGNO baocaocongnoEF)
      {
          Mapper.CreateMap<BAOCAOCONGNO, BaoCaoCongNoDTO>();
          return Mapper.Map<BAOCAOCONGNO,BaoCaoCongNoDTO>(baocaocongnoEF);
      }
      private static BAOCAOCONGNO MapBaoCaoCongNoDTOToBaoCaoCongNoEF(BaoCaoCongNoDTO baocaocongnoDTO)
      {
          Mapper.CreateMap<BaoCaoCongNoDTO, BAOCAOCONGNO>();
          return Mapper.Map<BaoCaoCongNoDTO, BAOCAOCONGNO>(baocaocongnoDTO);
      }
      public static List<BaoCaoCongNoDTO> GetAllBaoCaoCongNo()
      {
          List<BaoCaoCongNoDTO> list=new List<BaoCaoCongNoDTO>();
          using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
          {
              try
              {
                  var query = from baocaocongno in context.BAOCAOCONGNOList
                              select baocaocongno;
                  foreach (var baocaocongnoEF in query)
                  {
                      BaoCaoCongNoDTO baocaocongnoDTO = MapBaoCaoCongNoEFToBaoCaoCongNoDTO(baocaocongnoEF);
                      list.Add(baocaocongnoDTO);
                  }
              }
              catch(Exception)
              {
              }
          }
          return list;
      }
      public void Insert(BaoCaoCongNoDTO baocaocongnoDTO)
      {
          using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
          {
              try
              {
                  BAOCAOCONGNO baocaocongnoEF = new BAOCAOCONGNO();
                  baocaocongnoEF = MapBaoCaoCongNoDTOToBaoCaoCongNoEF(baocaocongnoDTO);
                  context.AddToBAOCAOCONGNOList(baocaocongnoEF);
                  context.SaveChanges();

              }
              catch (Exception)
              {

              }
          }
      }
      public void Update(BaoCaoCongNoDTO baocaocongnoDTO)
      {
          using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
          {
              try
              {
                  EntityKey key = context.CreateEntityKey("BAOCAOCONGNOList", new BAOCAOCONGNO { MaBaoCaoCongNo = baocaocongnoDTO.MaBaoCaoCongNo });
                  BAOCAOCONGNO origbaocaocongno = context.GetObjectByKey(key) as BAOCAOCONGNO;
                  context.Detach(origbaocaocongno);
                  context.Attach(origbaocaocongno);
                  BAOCAOCONGNO updatebaocaocongno = MapBaoCaoCongNoDTOToBaoCaoCongNoEF(baocaocongnoDTO);
                  context.ApplyCurrentValues<BAOCAOCONGNO>("BAOCAOCONGNOList", updatebaocaocongno);
                  context.SaveChanges();

              }
              catch (Exception)
              {

              }
          }
      }

    }
}
